﻿@{
    ViewBag.Title = "备件需求清单明细";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    <script src="~/Areas/Dev/ViewModels/Dev.com.js"></script>
    <script src="~/Areas/Dev/ViewModels/Dev.viewModel.edit.js"></script>
    <script type="text/javascript">
            using(['numberbox','lookup']);
            var viewModel = function(){
                var self = this;
                Dev.viewModel.edit.apply(self,arguments);
                this.grid.size={w:6,h:151};
                this.grid.OnAfterCreateEditor = function(editors,row){
                    Dev.com.bindCalcTotalMoney(self, "Bjqdmx_Sl", "Bjqdmx_Dj", "Bjqdmx_Je")(editors);
//                    $.fn.validatebox.defaults.rules.checkNum = {
//                        validator:function(value,param){return parseFloat(value) <= parseFloat(row.SrcNum);},
//                        message:'退库数量不能大于出库数量！'
//                    };
                };

                var calcs = function () {

                    alert("ss");
                };
               var  ss = $("#Bjqd_Jzrq");
               ss.blur(calcs);

                 this.saveClick = function () {
                    //保存主表及明细表
                    if (self.readonly()) return;
                    self.gridEdit.ended(); //结束grid编辑状态
                    //        var s = $('#Ldphbz_Dwid').combobox('getValues');
                    var post = {           //传递到后台的数据
                        form: com.formChanges(self.form, data.form, self.setting.postFormKeys),
                        list: self.gridEdit.getChanges(self.setting.postListFields)
                    };
                    if ((self.gridEdit.ended() && com.formValidate()) && (post.form._changed || post.list._changed)) {
                        com.ajax({
                            url: self.urls.edit,
                            data: ko.toJSON(post),
                            success: function (d) {
                                com.message('success', self.resx.editSuccess);
                                ko.mapping.fromJS(post.form, {}, data.form); //更新旧值
                                self.gridEdit.accept();
                            }
                        });
                    }

                    };
                this.addRowClick = function(){
                    if (self.readonly()) return;
//                        data: { BillNo: sBillNo },
//                        success: function (d) {
//                            var row = { Bjqdmx_BillNo: sBillNo, Bjqdmx_Flbh: d };
//                            self.gridEdit.addnew(row);
//                        }
//                    });
//                    var param = {_xml:"mms.material_send",WarehouseCode:self.form.WarehouseCode(),PickUnit:self.form.RefundUnit()};
//                    mms.com.selectMaterial(self,param);


//
 var grid = self.grid;


  var param = {_xml:"mms.material_send"};
    var addnew = self.gridEdit.addnew;
    var defaultRow = self.defaultRow;
    var url = self.urls.getrowid;
    //明细行-单据编号
    defaultRow.Bjqdmx_BillNo = self.scrollKeys.current();

    //var isExist = {}, existData = grid.datagrid('getData').rows;
    //for (var j in existData)
    //    isExist[existData[j].MaterialCode] = true;
    var orgRows = grid.datagrid('getData').rows;
//    var comapreArray = param._xml == "mms.material_batches" ? ['MaterialCode', 'SrcBillType', 'SrcBillNo'] : ['MaterialCode'];
    var comapreArray =['Wlzd_Wlbh'];
    var fnEqual = function (row1, row2) {
        for (var key in comapreArray)
            if (row1[comapreArray[key]] != row2[comapreArray[key]])
                return false;
        return true;
    }
    var fnExist = function (row) {
        for (var i in orgRows)
            if (fnEqual(orgRows[i], row))
                return true;
        return false;
    };

    var target = parent.$('#selectWlzd').length ? parent.$('#selectWlzd') : parent.$('<div id="selectWlzd"></div>').appendTo('body');
    utils.clearIframe(target);

    var opt = { title: '物料字典', width: 800, height: 550, modal: true, collapsible: false, minimizable: false, maximizable: true, closable: true };
    opt.content = "<iframe id='frm_win_material' src='/Basic/home/LookupWlzd' style='height:100%;width:100%;border:0;' frameborder='0'></iframe>";  //frameborder="0" for ie7
    opt.paramater = param;      //可传参数
    opt.onSelect = function (data) {                //可接收选择数据
        var total = data.total;
        var rows = data.rows;
        com.ajax({
            type: 'GET',
            url: url + total,
            data: { BillNo: self.form.Bjqd_BillNo() },
            success: function (d) {
                var ids = d.split(',');
                for (var i in rows) {
                   var row = { Bjqdmx_Wlbh:rows[i]["Wlzd_Wlbh"],Wlzd_Wlmc:rows[i]["Wlzd_Wlmc"],Wlzd_Ggxh:rows[i]["Wlzd_Ggxh"] };
//                    if (!fnExist(rows[i]))
                          addnew($.extend({ Bjqdmx_Flbh: ids[i] }, defaultRow, row));
                }
            }
        });
       
    };
    target.window(opt);
              };  
            };
            var data = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));
            ko.bindingViewModel(new viewModel(data));
    </script>
}

    <div class="z-toolbar">
        <a id="a_save" href="#" plain="true" class="easyui-linkbutton" icon="icon-save" data-bind="click:saveClick,linkbuttonDisable:readonly" title="保存">保存</a>
        <a id="a_undo" href="#" plain="true" class="easyui-linkbutton" icon="icon-undo" data-bind="click:rejectClick,linkbuttonDisable:readonly" title="撤消">撤消</a>
        <a id="a_audit" href="#" plain="true" class="easyui-linkbutton" icon="icon-user-accept" data-bind="click:auditClick,easyuiLinkbutton:approveButton" title="审核">审核</a>
        <a id="a_printer" href="#" plain="true" class="easyui-linkbutton" icon="icon-printer" title="打印" data-bind="click:printClick">打印</a>
        <div class="datagrid-btn-separator"></div>
        <a id="a_first" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_first" data-bind="click:firstClick,linkbuttonEnable:scrollKeys.firstEnable" title="第一条"></a> 
        <a id="a_previous" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_previous" data-bind="click:previousClick,linkbuttonEnable:scrollKeys.previousEnable" title="上一条"></a> 
        <a id="a_next" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_next" data-bind="click:nextClick,linkbuttonEnable:scrollKeys.nextEnable" title="下一条"></a> 
        <a id="a_last" href="#" plain="true" class="easyui-linkbutton" icon="icon-resultset_last" data-bind="click:lastClick,linkbuttonEnable:scrollKeys.lastEnable" title="最后一条"></a> 
    </div>

    <div id="master" class="container_12" data-bind="inputwidth:0.9">
        <div class="grid_1 lbl">单据编号</div>
        <div class="grid_3 val"><input type="text" id="Bjqd_BillNo" name="Bjqd_BillNo"  data-bind="value:form.Bjqd_BillNo,readOnly:true" class="z-txt readonly"/></div>
        <div class="grid_1 lbl">截止日期</div>
        <div class="grid_3 val"><input type="text" id="Bjqd_Jzrq" name="Bjqd_Jzrq" data-bind="dateboxValue:form.Bjqd_Jzrq,dateboxReadOnly:readonly" class="z-txt easyui-datebox" /></div>
        <div class="grid_1 lbl required">使用部门</div>
        <div class="grid_3 val"><input type="text"  data-bind="lookupValue:form.Bjqd_Bmbh,lookupReadOnly:readonly" required="true" class="z-txt easyui-lookup" data-options="lookupType:'organize',parentField:'pid',multiple:false"/></div>
               
        <div class="clear"></div>
        

        <div class="grid_1 lbl">设备编号 </div>
        <div class="grid_3 val"><input type="text"  data-bind="lookupValue:form.Bjqd_Sbbh,lookupReadOnly:readonly" required="true" class="z-txt easyui-lookup" data-options="lookupType:'sbda',parentField:'pid',multiple:false"/></div>
        <div class="grid_1 lbl">需求日期</div>
        <div class="grid_3 val"><input type="text" id="Bjqd_Xqrq" name="Bjqd_Jzrq" data-bind="dateboxValue:form.Bjqd_Xqrq,dateboxReadOnly:readonly" class="z-txt easyui-datebox" /></div>
        <div class="grid_1 lbl">建议供应商 </div>
        <div class="grid_3 val required"><input type="text" data-bind="value:form.Bjqd_Jygys,readOnly:readonly" class="z-txt"  /></div>
        <div class="clear"></div>

        <div class="grid_1 lbl">备注说明 </div>
        <div class="grid_11 val required"><input type="text" data-bind="value:form.Bjqd_Remark,readOnly:readonly" class="z-txt"  /></div>
        <div class="clear"></div>
    </div>
  
    <div id="tt" class="easyui-tabs">  
        <div title="表单明细">
            <table id="list" data-bind="datagrid:grid">
                <thead>
                    <tr>
                        <th field="Bjqdmx_BillNo" hidden="true"></th>  
                        <th field="Bjqdmx_Flbh"   align="left"    width="60"  >分录编号</th> 
                        <th field="Bjqdmx_Wlbh"   align="left"    width="80"  >物料编码   </th>  
                        <th field="Wlzd_Wlmc"     align="left"    width="150"  >物料名称   </th>  
                        <th field="Wlzd_Ggxh"     align="left"    width="80"   >规格型号   </th>  
                        <th field="Bjqdmx_Sl"     align="right"   width="100"  editor="{type: 'numberbox',options:{min: 0}}">需求数量</th>  
                        <th field="Bjqdmx_Dj"     align="right"   width="100"  editor="{type: 'numberbox',options:{min: 0, precision: 2}}"  formatter="com.formatMoney"">单价</th>  
                        <th field="Bjqdmx_Je"     align="right"   width="100"  editor="{type: 'numberbox',options:{min: 0, precision: 2}}"  formatter="com.formatMoney"">金额</th>  
                    </tr>
                </thead>
            </table>  
            
            <div id="gridtb">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" data-bind="click:addRowClick,linkbuttonDisable:readonly">选择物料</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" data-bind="click:removeRowClick,linkbuttonDisable:readonly">删除明细</a>
            </div>  
        </div>
        <div title="表单信息" class="hide" style="padding-top:2px;">
            <div class="container_12" id="BillDetail" data-bind="inputwidth:0.9,autoheight:181"> 
                <div class="clear"></div>
                <div class="grid_1 lbl">审批状态</div>
                <div class="grid_3 val"><input type="text" id="ApproveState" data-bind="value:form.ApproveState,readOnly:true" class="z-txt readonly"/></div>
                <div class="grid_1 lbl">审批意见</div>
                <div class="grid_3 val"><input type="text" id="ApproveRemark" data-bind="value:form.ApproveRemark,readOnly:true"  class="z-txt readonly"/></div>
                <div class="grid_1 lbl">审批人 </div>
                <div class="grid_3 val"><input type="text" id="ApprovePerson"data-bind="value:form.ApprovePerson,readOnly:true" class="z-txt readonly"/></div>

                <div class="clear"></div>
                <div class="grid_1 lbl">审批日期</div>
                <div class="grid_3 val"><input type="text" id="ApproveDate" data-bind="dateboxValue:form.ApproveDate,dateboxReadOnly:true"  class="z-txt easyui-datebox readonly"/></div>
                <div class="grid_1 lbl">编制日期</div>
                <div class="grid_3 val"><input type="text" id="CreateDate" data-bind="dateboxValue:form.CreateDate,dateboxReadOnly:true" class="z-txt easyui-datebox readonly" /></div>
                <div class="grid_1 lbl">编制人</div>
                <div class="grid_3 val "><input type="text" id="CreatePerson" data-bind="value:form.CreatePerson,readOnly:true" class="z-txt readonly" /></div>
                
                <div class="clear"></div>
                <div class="grid_1 lbl">修改日期</div>
                <div class="grid_3 val"><input type="text" id="UpdateDate" data-bind="dateboxValue:form.UpdateDate,dateboxReadOnly:true" class="z-txt easyui-datebox readonly" /></div>
                <div class="grid_1 lbl">修改人</div>
                <div class="grid_3 val "><input type="text" id="UpdatePerson" data-bind="value:form.UpdatePerson,readOnly:true" class="z-txt readonly" /></div>
            </div>
        </div> 
    </div>  

